package cn.blue.org.controller;
import cn.blue.base.util.ExcelUtil;
import cn.blue.org.domain.ShopAuditLog;
import cn.blue.org.service.IShopService;
import cn.blue.org.domain.Shop;
import cn.blue.org.query.ShopQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.PageList;
import com.sun.deploy.net.HttpResponse;
import jdk.jfr.events.ExceptionThrownEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    //店铺登录校验
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙请重试");
        }


    }

    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    //用户驳回回显接口
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {


        return shopService.findById(id);

    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public List<Shop> list() {

        return shopService.findAll();
    }
    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }
    //审核通过提交接口
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.auditPass(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙请重试");
        }
    }
    //审核通过 用户点击链接请求接口
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        System.out.println(id);
        try {
            shopService.active(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙请重试");
        }
    }
    //审核驳回接口
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.auditReject(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙请重试");
        }

    }
    //导出表格
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse resp) {
        //1查询所有数据
        List<Shop> shops = shopService.findAll();
        //使用excel进行导出
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",resp);

    }
    //导入表格
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        //调用工具类进行导入
        List<Shop>shops = ExcelUtil.importExcel(file,0,1,Shop.class);
        //调用批量新增方法
        System.out.println(shops.size());
    }




}
